﻿using FolderBrowser;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace SuperKit
{
    /// <summary>
    /// SettingsWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SettingsWindow : Window {
        FolderBrowserDialog folderBrowser;
        OpenFileDialog fileDialog;

        public static ConfigReader Cfg => ConfigReader.Instance;

        public SettingsWindow() {
            InitializeComponent();
        }

        private void OpenConfig_Click(object sender, RoutedEventArgs e) {
            System.Diagnostics.Process.Start("notepad.exe", ConfigReader.CONFIG_PATH);
        }

        private void OpenCommand_Click(object sender, RoutedEventArgs e) {
            System.Diagnostics.Process.Start("notepad.exe", ConfigReader.COMMAND_PATH);
        }

        private void OpenApp_Click(object sender, RoutedEventArgs e) {
            System.Diagnostics.Process.Start("explorer.exe", Kit.AppPath);
        }

        private void OK_Click(object sender, RoutedEventArgs e) {
            SaveConfig();
            Close();
        }

        private void Cancel_Click(object sender, RoutedEventArgs e) {
            Close();
        }

        private bool Is(string key) {
            return Cfg.GetBool(key);
        }

        private void Set(string key, bool value) {
            Cfg.SetBoolean(key, value);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {
            LoadConfig();
        }

        private void LoadConfig() {
            checkUseSystemVideoPlayer.IsChecked = Cfg.UseSystemVideoPlayer;
            txtDefaultDirectory.Text = Cfg.DefaultDirectory;
            txt7ZPath.Text = Cfg.ZipExe;
            txtFFMpegPath.Text = Cfg.FFMpegExe;
            txtPythonPath.Text = Cfg.PythonExe;
            txtImageCatalogPath.Text = Cfg.FastImageCatalog;
            checkSingleClickPlay.IsChecked = Cfg.SingleClickPlay;
            txtDefaultBytesRate.Text = $"{Cfg.DefaultBytesRate:N2}";
            txtVideoMakeThread.Text = Cfg.VideoMakeThread.ToString();
            txtGalleryThread.Text = Cfg.GalleryThread.ToString();


            LoadCatalog(listBoxCatalog, Cfg.Catalog);

            var startup = Cfg.StartupWindow;
            radio_Startup_Default.IsChecked = Kit.IsEmpty(startup) || Kit.IsEqual(startup, App.STARTUP_DEFAULT);
            radio_Startup_Rename.IsChecked = Kit.IsEqual(startup, App.STARTUP_RENAME);
            radio_Startup_Gallery.IsChecked = Kit.IsEqual(startup, App.STARTUP_GALLERY);
            radio_Startup_Video.IsChecked = Kit.IsEqual(startup, App.STARTUP_VIDEO);
            radio_Startup_HTML.IsChecked = Kit.IsEqual(startup, App.STARTUP_HTML);
        }
        private void SaveConfig() {
            Cfg.UseSystemVideoPlayer = checkUseSystemVideoPlayer.IsChecked == true;
            Cfg.DefaultDirectory = txtDefaultDirectory.Text;
            Cfg.ZipExe = txt7ZPath.Text;
            Cfg.FFMpegExe = txtFFMpegPath.Text;
            Cfg.PythonExe = txtPythonPath.Text;
            Cfg.FastImageCatalog = txtImageCatalogPath.Text;
            Cfg.SingleClickPlay = checkSingleClickPlay.IsChecked == true;
            Cfg.DefaultBytesRate = SafeParseFloat(txtDefaultBytesRate.Text);
            Cfg.VideoMakeThread = SafeParseInt(txtVideoMakeThread.Text);
            Cfg.GalleryThread = SafeParseInt(txtGalleryThread.Text);

            Cfg.Catalog = SaveCatalog(listBoxCatalog);

            var startup = App.STARTUP_DEFAULT;
            startup = radio_Startup_Rename.IsChecked == true ? App.STARTUP_RENAME : startup;
            startup = radio_Startup_Gallery.IsChecked == true ? App.STARTUP_GALLERY : startup;
            startup = radio_Startup_Video.IsChecked == true ? App.STARTUP_VIDEO : startup;
            startup = radio_Startup_HTML.IsChecked == true ? App.STARTUP_HTML : startup;
            Cfg.StartupWindow = startup;

            Cfg.Save();
        }


        private void LoadCatalog(ListBox list, string str) {
            list.Items.Clear();
            if (Kit.IsEmpty(str)) {
                return;
            }
            var arr = str.Split(Kit.CATALOG_SPLIT);
            foreach (var i in arr) {
                list.Items.Add(i);
            }
        }

        private string SaveCatalog(ListBox list) {
            string[] arr = new string[list.Items.Count];
            for (int i = 0; i < arr.Length; i++) {
                arr[i] = list.Items[i].ToString();
            }
            return string.Join("|", arr);
        }

        private int SafeParseInt(string str) {
            int.TryParse(str, out int out1);
            return out1;
        }

        private float SafeParseFloat(string str) {
            float.TryParse(str, out float out1);
            return out1;
        }

        private void buttonBrowseDefaultDirectory_Click(object sender, RoutedEventArgs e) {
            folderBrowser ??= new FolderBrowserDialog();

            folderBrowser.ViewModel.SelectedFolder = txtDefaultDirectory.Text;
            if (folderBrowser.ShowDialog() == true) {
                txtDefaultDirectory.Text = folderBrowser.SelectedFolder;
            }
        }

        private void buttonBroswe7Z_Click(object sender, RoutedEventArgs e) {
            fileDialog ??= new OpenFileDialog();

            fileDialog.Filter = "可执行文件(*.exe)|*.exe|所有文件(*.*)|*.*";
            fileDialog.InitialDirectory = Path.GetDirectoryName(txt7ZPath.Text);
            fileDialog.FileName = txt7ZPath.Text;
            if (fileDialog.ShowDialog() == true) {
                txt7ZPath.Text = fileDialog.FileName;
            }
        }

        private void buttonBrowseFFMpeg_Click(object sender, RoutedEventArgs e) {
            fileDialog ??= new OpenFileDialog();

            fileDialog.Filter = "可执行文件(*.exe)|*.exe|所有文件(*.*)|*.*";
            fileDialog.InitialDirectory = Path.GetDirectoryName(txtFFMpegPath.Text);
            fileDialog.FileName = txtFFMpegPath.Text;
            if (fileDialog.ShowDialog() == true) {
                txtFFMpegPath.Text = fileDialog.FileName;
            }
        }

        private void buttonBrowsePython_Click(object sender, RoutedEventArgs e) {
            fileDialog ??= new OpenFileDialog();

            fileDialog.Filter = "可执行文件(*.exe)|*.exe|所有文件(*.*)|*.*";
            fileDialog.InitialDirectory = Path.GetDirectoryName(txtPythonPath.Text);
            fileDialog.FileName = txtPythonPath.Text;
            if (fileDialog.ShowDialog() == true) {
                txtPythonPath.Text = fileDialog.FileName;
            }
        }

        private void buttonImageCatalogPath_Click(object sender, RoutedEventArgs e) {
            folderBrowser ??= new FolderBrowserDialog();

            folderBrowser.ViewModel.SelectedFolder = txtImageCatalogPath.Text;
            if (folderBrowser.ShowDialog() == true) {
                txtImageCatalogPath.Text = folderBrowser.SelectedFolder;
            }
        }

        private void buttonAddFastEditCatalog_Click(object sender, RoutedEventArgs e) {
            var list = listBoxCatalog;
            list.Items.Add("目录");
        }

        private void buttonDeleteFastEditCatalog_Click(object sender, RoutedEventArgs e) {
            var list = listBoxCatalog;
            if (list.SelectedIndex != -1) {
                list.Items.RemoveAt(list.SelectedIndex);
            }
        }

        private void buttonEditFastEditCatalog_Click(object sender, RoutedEventArgs e) {
            var list = listBoxCatalog;
            if (list.SelectedIndex != -1) {
                SimpleTextEditor dlg = new SimpleTextEditor {
                    EditText = (string)list.SelectedItem
                };
                if (dlg.ShowDialog() == true) {
                    list.Items[list.SelectedIndex] = dlg.EditText;
                }
            }
        }

        private void buttonOpenConfigFile_Click(object sender, RoutedEventArgs e) {
            System.Diagnostics.Process.Start(ConfigReader.CONFIG_PATH);
        }

        private void buttonOpenCommandXml_Click(object sender, RoutedEventArgs e) {
            System.Diagnostics.Process.Start(ConfigReader.COMMAND_PATH);
        }

    }
}
